package com.lw.sbdemo2.web;

import com.lw.sbdemo2.common.base.BaseController;
import com.lw.sbdemo2.common.constant.TkConstants;
import com.lw.sbdemo2.common.utils.IdUtils;
import com.lw.sbdemo2.common.vo.PageInfo;
import com.lw.sbdemo2.common.web.anno.Required;
import com.lw.sbdemo2.common.web.anno.ResultController;
import com.lw.sbdemo2.entity.TkExamProductMaster;
import com.lw.sbdemo2.entity.TkOrderRecord;
import com.lw.sbdemo2.entity.TkUserExamRecord;
import com.lw.sbdemo2.service.TkExamProductMasterService;
import com.lw.sbdemo2.service.TkOrderRecordService;
import com.lw.sbdemo2.service.TkUserExamRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>标题: 订单记录表服务 - 请求控制层</p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2019</p>
 * <p>公司: 山人行工作室</p>
 *
 * @version: 1.0
 * @author: LinWang
 * @date 2019-4-14
 */
@ResultController
@Api(description = "TkOrderRecordController", tags = {"订单记录表服务"})
@RequestMapping(value = "/tkOrderRecord")
public class TkOrderRecordController extends BaseController {

    /**
     * 订单记录表服务
     */
    @Autowired
    private TkOrderRecordService tkOrderRecordService;

    @Autowired
    private TkUserExamRecordService tkUserExamRecordService;

    @ApiOperation(value = "创建订单")
    @PostMapping("/orderCreate")
    public String orderCreate(TkOrderRecord tkOrderRecord) {
        return tkOrderRecordService.orderCreate(tkOrderRecord);
    }

    @ApiOperation(value = "获取订单记录表列表")
    @GetMapping("")
    public List<TkOrderRecord> findList(TkOrderRecord tkOrderRecord) {
        return tkOrderRecordService.findList(tkOrderRecord);
    }

    @ApiOperation(value = "获取订单记录表列表（分页）", response = PageInfo.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "query", value = "关键词", dataType = "string"),
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int"),
    })
    @GetMapping(value = "/page")
    public PageInfo<TkOrderRecord> page(String query, @Required("pageNum") PageInfo pageInfo, TkOrderRecord tkOrderRecord) {
        return tkOrderRecordService.findListPage(query, pageInfo, tkOrderRecord);
    }

    @ApiOperation(value = "获取订单记录表详细信息", notes = "根据url的id来获取订单记录表详细信息")
    @ApiImplicitParam(name = "id", value = "订单记录表ID", required = true, dataType = "String")
    @GetMapping("/{id}")
    public TkOrderRecord get(@PathVariable String id) {
        return tkOrderRecordService.getById(id);
    }

    @ApiOperation(value = "创建订单记录表")
    @PostMapping("/insert")
    public void insert(TkOrderRecord tkOrderRecord) {
        tkOrderRecord.setCreatedTime(new Date());
        tkOrderRecord.setOrderId(IdUtils.uuid());
        tkOrderRecordService.insert(tkOrderRecord);
    }

    /**
     * 用户试卷记录表服务
     */
    @Autowired
    private TkExamProductMasterService tkExamProductMasterService;


    @ApiOperation(value = "更新订单记录表详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单记录表ID", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/update")
    public void update(TkOrderRecord tkOrderRecord) {
        tkOrderRecord.setModifiedTime(new Date());

        //状态为支付的修改
        String status = tkOrderRecord.getOrderStatus();
        BigDecimal orderMonry = tkOrderRecord.getOrderMonry();
        if (TkConstants.ORDER_STATUS_PADED.equals(status) && BigDecimal.ZERO.compareTo(orderMonry) == 0) {

            //订单数据更新
            tkOrderRecord.preUpdate(tkOrderRecord.getOrderId(), BigDecimal.ZERO, "");
            tkOrderRecordService.update(tkOrderRecord);

            //插入用户拥有事件记录
            TkOrderRecord syOrderMasterTemp = tkOrderRecordService.getById(tkOrderRecord.getOrderId());
            TkUserExamRecord tkUserExamRecord = new TkUserExamRecord();
            tkUserExamRecord.setUserId(syOrderMasterTemp.getUserId());
            tkUserExamRecord.setExamId(syOrderMasterTemp.getExamId());
            tkUserExamRecord.setBuyTime(new Date());
            TkExamProductMaster tkExamProductMaster = tkExamProductMasterService.getById(syOrderMasterTemp.getExamId());
            if (tkExamProductMaster != null) {
                tkUserExamRecord.setExamName(tkExamProductMaster.getExamName());
            }
            tkUserExamRecordService.insert(tkUserExamRecord);
        } else {
            tkOrderRecordService.update(tkOrderRecord);
        }
    }

    @ApiOperation(value = "删除订单记录表", notes = "删除订单记录表")
    @ApiImplicitParam(name = "id", value = "订单记录表ID", required = true, paramType = "query", dataType = "String")
    @PostMapping("/delete")
    public void deleteTkOrderRecord(TkOrderRecord tkOrderRecord) {

        tkOrderRecordService.delete(tkOrderRecord);
        TkUserExamRecord tkUserExamRecord = TkUserExamRecord.builder()
                                            .examId(tkOrderRecord.getExamId())
                                            .userId(tkOrderRecord.getUserId()).build();
        tkUserExamRecordService.delete(tkUserExamRecord);
    }

}
